Ext.define('EM.controller.Events', {
    extend:'Ext.app.Controller',
    models:['Event', 'Folder'],
    stores:['Events', 'Folders'],
    views:['Viewport', 'Body'],

    init:function () {

        var me = this;

        this.control({
            '#events-silder':{
                change:function (s, v) {
                    me.onSlide(v);
                },
                changecomplete:function (s, v) {
                    me.onSlide(v)
                }
            },
            '#events-filter-button':{
                click:function () {
                    var filterPanel = Ext.getCmp('events-filter-panel');
                    if (filterPanel.hidden) {
                        filterPanel.show();
                    } else {
                        filterPanel.hide();
                    }
                }
            },
            '#events-search-toolbar > textfield':{
                keyup:function (field, e) {
                    var value = field.getValue();
                    me.onFilter('Name', value);
                },
                specialkey:function (field, e) {
                    if (e.getKey() === e.ESC) {
                        field.reset();
                        me.onFilter();
                    }
                }
            },
            '#events-expand-button':{
                click:function () {
                    Ext.getCmp('events-timeline-view').expandAll();
                }
            },
            '#events-collapse-button':{
                click:function () {
                    Ext.getCmp('events-timeline-view').collapseAll();
                }
            },
            '#events-zoomtofit-button':{
                click:function () {
                    Ext.getCmp('events-timeline-view').zoomToFit();
                }
            },
            '#events-save-button':{
                click:function () {
                    Ext.getStore('Events').sync();
                }
            },
            '#events-timeline-view > treecolumn':{
                edit: function() {
//                    alert("edited");
                }
            },
            '#events-timeline-view':{
                taskclick: function(gantt, taskRecord, e, eOps) {
//                    alert("task clicked");
                },
                taskdrop: function(gantt, taskRecord, e, eOps) {
                    // after successfull drag
//                    alert('task drop');
                },
//                labeledit_beforecomplex: function() {
//                    Ext.getStore('Events').sync();
//                },
                labeledit_complete: function(gantt, value, startValue, taskRecord) {

                },
                taskcontextmenu:function(gannt, taskRecord, e, eOps){
                    e.preventDefault();
                    Ext.create('Gnt.plugin.TaskContextMenu').showAt(e.getXY());
                }
            }
        });
    },

    onFilter:function (field, value) {
        if (field && value) {
            Ext.getStore('Events').filter(field, value, true, false);
        } else {
            Ext.getStore('Events').clearFilter();
        }
    },

    onSlide:function (colWidth) {
        Ext.getCmp('events-timeline-view').setTimeColumnWidth(colWidth);
    }
});
